﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DungeonDelver.Common
{
    public class BoardWalker<T>:IBoardWalker<T>
    {
        private HashSet<T> directions;
        public IEnumerable<T> Directions
        {
            get
            {
                return directions;
            }
            set
            {
                directions = new HashSet<T>(value);
            }
        }

        public Func<int, int, T, int> NextColumnLambda
        {
            get;
            set;
        }

        public Func<int, int, T, int> NextRowLambda
        {
            get;
            set;
        }

        public int GetNextColumn(int theColumn, int theRow, T theDirection)
        {
            if (!directions.Contains(theDirection))
            {
                throw new IndexOutOfRangeException();
            }
            return NextColumnLambda(theColumn, theRow, theDirection);
        }

        public int GetNextRow(int theColumn, int theRow, T theDirection)
        {
            if (!directions.Contains(theDirection))
            {
                throw new IndexOutOfRangeException();
            }
            return NextRowLambda(theColumn, theRow, theDirection);
        }

        public BoardWalker() { }

        public BoardWalker(IEnumerable<T> theDirections, Func<int, int, T, int> theNextColumnLambda, Func<int, int, T, int> theNextRowLambda)
        {
            Directions = theDirections;
            NextColumnLambda = theNextColumnLambda;
            NextRowLambda = theNextRowLambda;
        }
    }
}
